package com.mentalhealthplatform.global.rbac.dto.normal;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.mentalhealthplatform.global.rbac.model.Menu;
import lombok.Getter;
import lombok.Setter;

import java.util.Set;
import java.util.stream.Collectors;

@Setter
@Getter
public class MenuDTO {
    private Long id;
    private Long parentId;
    private String identifier;
    private String displayName;
    private String url;
    private String icon;
    private Integer orderNum;
    private boolean visible;
    private boolean status;
    private String description;
    // "目录" 或 "菜单"
    private String type;

    @JsonIgnore
    private Set<MenuDTO> childMenus;
    @JsonIgnore
    private Set<ButtonDTO> buttons;
    @JsonIgnore
    private Set<ButtonComponentDTO> buttonComponents;



    @JsonProperty("childMenus")
    public Set<MenuDTO> getChildMenus() {
        return childMenus;
    }

    @JsonProperty("buttons")
    public Set<ButtonDTO> getButtons() {
        return buttons;
    }

    @JsonProperty("buttonComponents")
    public Set<ButtonComponentDTO> getButtonComponents() {
        return buttonComponents;
    }

    // 默认构造方法
    public MenuDTO() { }

    // 全参构造方法
    public MenuDTO(Long id, String identifier, String displayName,String type, String url, String icon, Integer orderNum, boolean visible, String description, boolean status, Set<MenuDTO> childMenus, Set<ButtonDTO> buttons) {
        this.id = id;
        this.displayName = displayName;
        this.type = type;
        this.identifier = identifier;
        this.url = url;
        this.icon = icon;
        this.orderNum = orderNum;
        this.visible = visible;
        this.description = description;
        this.status = status;
        this.childMenus = childMenus;
        this.buttons = buttons;
    }

    // 基于 Menu 实体的构造方法
    public MenuDTO(Menu menu) {
        this.id = menu.getId();
        this.identifier = menu.getIdentifier();
        this.type = menu.getType();
        this.displayName = menu.getDisplayName();
        this.url = menu.getUrl();
        this.icon = menu.getIcon();
        this.orderNum = menu.getOrderNum();
        this.visible = menu.isVisible();
        this.status = menu.isStatus();
        this.description = menu.getDescription();
        this.parentId = menu.getParentMenu() != null ? menu.getParentMenu().getId() : null;

        // 递归构建子菜单的 MenuDTO
        this.childMenus = menu.getChildMenus().stream()
                .map(MenuDTO::new)
                .collect(Collectors.toSet());

    }
}
